Page({
  data: {
    educationData:{},
    education:[],
    educationIndex:'',
    educationDegree: [],
    modify:'',
    schoolBeginDefault: true,
    schoolEndDefault: true,
    degreeDefault:true,
    schoolBeginDate: '2011-09-01',
    schoolEndDate: '2011-09-01',
    degree:['博士','硕士','本科'],
    degreeIndex:0,
    wrongTip: "内容有误"
  },
  onLoad: function (options) {
    var that = this;
    if (options.educationData){
      console.log(options);
      console.log(options.educationData)
      var educationData = JSON.parse(options.educationData);
      console.log(options.educationData)
      this.setData({
        modify:true,
        educationIndex:options.educationIndex,
        educationData: educationData,
        schoolBeginDate:educationData.schoolBeginDate,
        schoolEndDate:educationData.schoolEndDate
      })
      if (that.data.educationData.degree) {
        that.setData({
          degreeDefault: false
        })
      }
      if (that.data.educationData.schoolBeginDate) {
        that.setData({
          schoolBeginDefault: false
        })
      }
      if (that.data.educationData.schoolEndDate) {
        that.setData({
          schoolEndDefault: false
        })
      }
    }else{
      this.setData({
        modify:false,
        educationData: {},
        schoolBeginDate:'2011-09-01',
        schoolEndDate:'2011-09-01',
      })
    }
    wx.getStorage({
      key:'education',
      success:function(res){
        that.setData({
          education:res.data
        })
      }
    })
  },
  bindDegreeChange: function (e) {
    this.setData({
      degreeIndex: e.detail.value,
      degreeDefault: false,
    })
  },
  bindSchoolBeginChange: function (e) {
    this.setData({
      schoolBeginDate: e.detail.value,
      schoolBeginDefault: false,
    })
  },
  bindSchoolEndChange: function (e) {
    this.setData({
      schoolEndDate: e.detail.value,
      schoolEndDefault: false,
    })
  },
  formSubmit: function (e) {
    this.setData({
      educationData: e.detail.value,
    })
    if (!this.data.educationData.school) {
      wx.showModal({
        title: '提示',
        content: '请填写学校',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!this.data.educationData.major){
      wx.showModal({
        title: '提示',
        content: '请填写专业',
        showCancel: false,
        success: function (res) {
        }
      })
    }else if (this.data.degreeDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择您的学历',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.schoolBeginDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择你的入学时间',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.schoolEndDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择你的毕业时间',
        showCancel: false,
        success: function (res) {
        }
      })
    }else {
      if (this.data.modify == true) {
        this.data.educationDegree[this.data.educationIndex] = e.detail.value.degree;
        this.data.educationData.degree = this.data.degree[e.detail.value.degree];
        this.data.education[this.data.educationIndex] = this.data.educationData
      } else {
        this.data.educationDegree.push(e.detail.value.degree)
        this.data.educationData.degree = this.data.degree[e.detail.value.degree];
        this.data.education.push(this.data.educationData);
      }
      wx.setStorage({
        key: 'education',
        data: this.data.education,
        success: function (res) {
        }
      })
      wx.setStorage({
        key: 'educationDegree',
        data: this.data.educationDegree,
        success: function (res) {
        }
      });
      wx.request({
        url: "",
        data: this.data.education,
        header: { 'Content-Type': 'application/json' },
        success: function (res) {
          console.log(res.data)
        }
      });
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        educationInfo: currPage.data.educationData,
        education: currPage.data.education,
        educationShow: false,
        educationModify: true,
      });
      wx.showModal({
        title: '提示',
        content: '信息更新成功，请继续完善简历',
        showCancel: false,
        success: function (res) {
          wx.navigateBack()
        }
      })
    }
  },
  reset: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            educationData: {},
            schoolBeginDefault: true,
            schoolEndDefault: true,
            degreeDefault: true,
          })
          that.data.education.splice(that.data.educationIndex, 1);
          wx.setStorage({
            key: 'education',
            data: that.data.education,
            success: function (res) {
            }
          })
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1];
          var prevPage = pages[pages.length - 2];
          prevPage.setData({
            education: currPage.data.education,
          });
          if (that.data.education.length == 0) {
            prevPage.setData({
              educationShow: true
            });
          } else {
            prevPage.setData({
              educationShow: false
            });
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})